package com.example.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.common.Result;
import com.example.entity.*;
import com.example.service.*;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/echarts")
public class EchartsController {

    @Resource
    public CategoryService categoryService;
    @Resource
    public IntroductionService introductionService;
    @Resource
    public UserService userService;
    @Resource
    public DepartmentService departmentService;
    @Resource
    public FinancialService financialService;
    @Resource
    public SalaryService salaryService;


    @GetMapping("/pie")
    public Result pie() {
        List<Map<String, Object>> list = new ArrayList<>();
        // 查询出所有的分类信息(部门)
        List<Department> departments = departmentService.selectAll(new Department());
        // 查询出各部门资产信息的和
        List<Financial> finansicals = financialService.selectAll(new Financial());
        for (Department department : departments) {
            double sum = finansicals.stream()
                    .filter(x -> department.getId().equals(x.getDepartmentId()))
                    .mapToDouble(Financial::getPrice)
                    .sum();
            Map<String, Object> map = new HashMap<>();
            map.put("name", department.getName());
            map.put("value", sum);
            list.add(map);
        }
        return Result.success(list);
    }

    @GetMapping("/bar/{year}")
    public Result bar(@PathVariable(required = false) String year) {
        Map<String, Object> resultMap = new HashMap<>();
        List<String> xList = new ArrayList<>();
        List<Double> yList = new ArrayList<>();

        // 查出所有的部门
        List<Department> departments = departmentService.selectAll(new Department());
        for (Department department : departments) {
            xList.add(department.getName());
            Integer departmentId = department.getId();
            Double avgPrice = salaryService.selectAvgPrice(year,departmentId);
            yList.add(avgPrice);
        }

        resultMap.put("xAxis", xList);
        resultMap.put("yAxis", yList);
        return Result.success(resultMap);
    }

    @GetMapping("/line")
    public Result line() {
        Map<String, Object> resultMap = new HashMap<>();
        List<String> xList = new ArrayList<>();
        List<Double> yList = new ArrayList<>();

        // 查出所有的部门
        List<Department> departments = departmentService.selectAll(new Department());
        for (Department department : departments) {
            xList.add(department.getName());
        }

        // 查询出各部门资产信息的和
        List<Financial> finansicals = financialService.selectAll(new Financial());
        for (Department department : departments) {
            double sum = finansicals.stream()
                    .filter(x -> department.getId().equals(x.getDepartmentId()))
                    .mapToDouble(Financial::getPrice)
                    .sum();
            yList.add(sum);
        }

        resultMap.put("xAxis", xList);
        resultMap.put("yAxis", yList);
        return Result.success(resultMap);
    }

}
